package com.yifushidai.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value = "Res", description = "通用返回对象")
public class Res<T> {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "数据状态的字段名称，默认：code(成功的状态码，默认：0)", example = "0")
    private int code;

    @ApiModelProperty(value = "状态信息的字段名称，默认：msg", example = "success")
    private String msg;

    @ApiModelProperty(value = "数据列表的字段名称，默认：datas", example = "datas")
    private T datas;

    @ApiModelProperty(value = "数据总数的字段名称，默认：count", example = "666")
    private Long count;

    public Res() {
    }

    /*默认code + 默认msg  简易 error 和 ok*/
    public static Res ok() {
        Res res = new Res();
        res.code = ErrorCodeEnum.SUCCESS.getCode();
        res.msg = ErrorCodeEnum.SUCCESS.getName();
        return res;
    }

    public static Res error(int code, String msg) {
        Res res = new Res();
        res.code = code;
        res.msg = msg;
        return res;
    }

    public static Res error(ErrorCodeEnum errorCodeEnum) {
        return error(errorCodeEnum.getCode(), errorCodeEnum.getName());
    }

    public static Res error(String msg) {
        return error(ErrorCodeEnum.SYSTEM_ERROR.getCode(), msg);
    }

    public static Res error() {
        return error(ErrorCodeEnum.SYSTEM_ERROR);
    }

    public Res datas(T datas) {
        this.datas = datas;
        return this;
    }

    public Res count(Long count) {
        this.count = count;
        return this;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getDatas() {
        return datas;
    }

    public void setDatas(T datas) {
        this.datas = datas;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }
}
